package com.charger.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.charger.chargingStation.domain.Chargingstation;
import com.charger.chargingStation.service.IChargingstationService;
import com.charger.domain.Dispose;
import com.charger.service.IDisposeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.charger.common.annotation.Log;
import com.charger.common.core.controller.BaseController;
import com.charger.common.core.domain.AjaxResult;
import com.charger.common.enums.BusinessType;
import com.charger.domain.RisksManage;
import com.charger.service.IRisksManageService;
import com.charger.common.utils.poi.ExcelUtil;
import com.charger.common.core.page.TableDataInfo;

/**
 * 隐患处理Controller
 *
 * @author xuan
 * @date 2024-11-27
 */
@RestController
@RequestMapping("/risksFailure/risksDispose")
public class RisksManageController extends BaseController {
    @Autowired
    private IRisksManageService risksManageService;
    @Autowired
    private IDisposeService disposeService;
    @Autowired
    private IChargingstationService chargingstationService;

    /**
     * 查询隐患处理列表
     */
    @PreAuthorize("@ss.hasPermi('risksFailure:risksDispose:list')")
    @GetMapping("/list")
    public TableDataInfo list(RisksManage risksManage) {
        startPage();
        List<RisksManage> list = risksManageService.selectRisksManageList(risksManage);
        list.forEach(fai -> {
            // 修改处理表
            if (fai.getDisposalId() != null)
                fai.setDispose(disposeService.selectDisposeById(fai.getDisposalId()));
            // 修改充电站
            if (fai.getChargerId() != null)
                fai.setChargingstation(chargingstationService.selectChargingstationByChargingstationId(fai.getChargerId()));
        });
        return getDataTable(list);
    }

    /**
     * 导出隐患处理列表
     */
    @PreAuthorize("@ss.hasPermi('risksFailure:risksDispose:export')")
    @Log(title = "隐患处理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RisksManage risksManage) {
        List<RisksManage> list = risksManageService.selectRisksManageList(risksManage);
        ExcelUtil<RisksManage> util = new ExcelUtil<RisksManage>(RisksManage.class);
        util.exportExcel(response, list, "隐患处理数据");
    }

    /**
     * 获取隐患处理详细信息
     */
    @PreAuthorize("@ss.hasPermi('risksFailure:risksDispose:query')")
    @GetMapping(value = "/{risksId}")
    public AjaxResult getInfo(@PathVariable("risksId") Long risksId) {
        RisksManage risksManage = risksManageService.selectRisksManageByRisksId(risksId);
        // 修改处理表
        if (risksManage.getDisposalId() != null)
            risksManage.setDispose(disposeService.selectDisposeById(risksManage.getDisposalId()));
        // 修改充电站
        if (risksManage.getChargerId() != null)
            risksManage.setChargingstation(chargingstationService.selectChargingstationByChargingstationId(risksManage.getChargerId()));
        return success(risksManage);
    }

    /**
     * 新增隐患处理
     */
    @PreAuthorize("@ss.hasPermi('risksFailure:risksDispose:add')")
    @Log(title = "隐患处理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RisksManage risksManage) {
        return toAjax(risksManageService.insertRisksManage(risksManage));
    }

    /**
     * 修改隐患处理
     */
    @PreAuthorize("@ss.hasPermi('risksFailure:risksDispose:edit')")
    @Log(title = "隐患处理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RisksManage risksManage) {
        // 修改处理表
        Dispose dispose = risksManage.getDispose();
        disposeService.updateDispose(dispose);
        return toAjax(risksManageService.updateRisksManage(risksManage));
    }

    /**
     * 删除隐患处理
     */
    @PreAuthorize("@ss.hasPermi('risksFailure:risksDispose:remove')")
    @Log(title = "隐患处理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{risksIds}")
    public AjaxResult remove(@PathVariable Long[] risksIds) {
        return toAjax(risksManageService.deleteRisksManageByRisksIds(risksIds));
    }
}
